#include<iostream>
#include<fstream>

#include"contacts.pb.h"

using namespace std;

void PrintContacts(contacts2::Contacts& contacts){
    for(int i = 0;i < contacts.person_size();i++){
        const contacts2::PersonInfo& person = contacts.person(i);
        cout << "联系人姓名：" << person.name() << endl;
        cout << "联系人年龄：" << person.age() << endl;
        for(int j = 0;j < person.phone_size();j++){
            const contacts2::PersonInfo_Phone& phone = person.phone(j);
            cout << "联系人电话" << j + 1 << ":" << phone.phone_number() << endl;
            //联系人电话类型
            cout << "(" << phone.PhoneType_Name(phone.type()) << ")" << endl;
        }
        if(person.has_data() && person.data().Is<contacts2::Address>()){
            contacts2::Address address;
            person.data().UnpackTo(&address);
            if(!address.home_address().empty()){
                cout << "联系人家庭住址：" << address.home_address() << endl;
            }
            if(!address.unit_address().empty()){
                cout << "联系人家庭住址：" << address.unit_address() << endl;
            }
        }
        switch(person.other_contact_case()){
            case contacts2::PersonInfo::OtherContactCase::kQq:
                cout << "联系人qq:" << person.qq() << endl;
                break;
            case contacts2::PersonInfo::OtherContactCase::kWechat:
                cout << "联系人微信号：" << person.wechat() << endl;
                break;
            default:
                break;
        }

        if(person.remark_size()){
            cout << "备注:" << endl;
        }
        for(auto it = person.remark().cbegin();it != person.remark().cend();it++){
            cout << " " << it->first << ": " << it->second << endl; 
        }

    }
}



int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    contacts2::Contacts contact;

    //读取本地已存在的联系人的信息
    std::fstream istream("contacts.bin",std::ios::in | std::ios::binary);
    if(!contact.ParseFromIstream(&istream)){
        std::cerr << "parse false" << std::endl;
        istream.close();
        return -1;
    }

    //打印通讯录列表
    PrintContacts(contact);

    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}